package org.itboys.core.controller.front;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.core.entity.mongo.SundayContactMessage;
import org.itboys.core.service.mongo.front.SundayFrontContactMessageService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/21
 * Description: 联系客服小程序
 */

@Controller
@RequestMapping("/sunday/small/contactMessage")
@Api(value = "contactMessage", description = "联系客服")
public class SundayFrontContactMessageController extends BaseController {
    @Autowired
    private SundayFrontContactMessageService messageService;

    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ApiOperation(value = "保存用户反馈")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "memberid", value = "用户id, 必填")
            @RequestParam(value = "memberid", required = true) Long memberId
            ,
            @ApiParam(required = true, name = "memberName", value = "用户昵称")
            @RequestParam(value = "memberName", required = true) String memberName
            ,
            @ApiParam(required = true, name = "name", value = "问题标题")
            @RequestParam(value = "name", required = true) String name
            ,
            @ApiParam(required = false, name = "text", value = "问题标题")
            @RequestParam(value = "text", required = false) String text

    ) {
        try {
            messageService.save(memberId, memberName, name, text);
            ResponseMessage.success();
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/contactMessage/save----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @RequestMapping(value = "/select", method = {RequestMethod.POST})
    @ApiOperation(value = "提交用户反馈")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "memberid", value = "用户id, 必填")
            @RequestParam(value = "memberid", required = true) Long memberid
            ,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20")
            @RequestParam(value = "pageSize", required = true) String pageSize
            ,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1")
            @RequestParam(value = "pageNumber", required = true) String pageNumber
            ,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。")
            @RequestParam(value = "sort", required = false) String sort
            ,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序")
            @RequestParam(value = "order", required = false) String order
    ) {
        try {
            List<SundayContactMessage> result = messageService.select(request, memberid);
            ResponseMessage.success(result);
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/contactMessage/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @RequestMapping(value = "/getDetail", method = {RequestMethod.POST})
    @ApiOperation(value = "提交用户反馈")
    public void getDetail(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "id", value = "消息id, 必填")
            @RequestParam(value = "id", required = true) Long id

    ) {
        try {
            SundayContactMessage message = messageService.getById(id);
            ResponseMessage.success(message);
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/contactMessage/getDetail----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

}
